<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\library\Upload;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

class Order extends Api
{


    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function getgoodbyinfoid(){
        $id=$this->request->param('id');

        $info=Db::name('aa_photolog')
            ->where(['id'=>$id])
            ->find();

        if (!$info){
            $this->error('没有相关信息!');

        }
        $good_type=explode(' ',$info['good_type']);

        $info['good_type']=$good_type;
        $case=Db::name('aa_case')->where(['id'=>$info['caseid']])->find();
        $info['case']=$case['title'];


        $sku=explode('-',$info['sku']);

        if (is_array($sku)){
            $skustr=$sku[0];
            if(isset($sku[1])&&count($sku)>2){
                $skustr.='-'.$sku[1];
            }
        }else{
            $skustr='';
        }


        $tongkuan=Db::name('aa_photolog')
            ->where('sku','like','%'.$skustr.'%')
            ->where('caseid','eq',$info['caseid'])
            ->field(['sku','code','color1'])->select();

        $tktit=[];
        foreach ($tongkuan as $key){

            $tktit[]=$key['sku']."  " .$key['color'];

        }

        $info['tongkuan']=$tongkuan;
        $info['tktit']=$tktit;
        $info['tongkuancount']=count($tongkuan);


        $this->success('请求成功!', $info);

    }

    /**
     * 查询 详情
     */
    public function getgoodbyid()
    {
        $code=$this->request->param('codes');

        $infos = Db::name('aa_photolog')
            ->where('code', 'eq', $code)
            ->whereOr('sku', 'eq', $code)
            ->whereOr('title', 'eq', $code)
            ->limit('6')
            ->select();

        if (count($infos)<1){
            $this->error('没有相关信息!');
        }


        if (count($infos)>1){

            $array=[];
            foreach ($infos as &$kk){
                $arr=[];
                $pc=Db::name('aa_case')->where(['id'=>$kk['caseid']])->find();
                $arr['case']=$pc['title'];
                $arr['id']=$kk['id'];
                $tx=$kk['status']==5?' 退仓':' 未退';
                $arr['title']=$pc['title'].'-'.$kk['color'].$tx ;
                $array[]=$arr;
            }

            $this->success('多个信息',$array);
        }else{
            $info=$infos[0];
        }


        $case=Db::name('aa_case')->where(['id'=>$info['caseid']])->find();

        $info['case']=$case['title'];

        $sku=explode('-',$info['sku']);

        if (is_array($sku)){
            $skustr=$sku[0];
            if(isset($sku[1])&&count($sku)>2){
                $skustr.='-'.$sku[1];
            }
        }else{
            $skustr='';
        }


        $tongkuan=Db::name('aa_photolog')
            ->where('sku','like','%'.$skustr.'%')
            ->where('caseid','eq',$info['caseid'])
            ->field(['sku','code','color1','createtime','typeout'])->select();

        $tktit=[];
        foreach ($tongkuan as $key){
            $tx=$key['typeout']==1 ? ' 退仓' : ' 未退';
            $tktit[]=$key['sku']." ".mb_substr($key['color1'],0,9) . $tx;
        }

        $info['tongkuan']=$tongkuan;
        $info['tktit']=$tktit;
        $info['tongkuancount']=count($tongkuan);
        $this->success('请求成功!', $info);


    }

    public function getusertype($status){
//'', '', '',  '' , '', ''
        switch ($status){
            case 1:
                return '制作中';
                break;

            case 2:
                return '校对中';
                break;

            case 3:
                return '已交图';
                break;
            case 4:
                return '待返员';
                break;
            case 5:
                return '已返货';
                break;

            default:
                return  '入库中';
        }


    }


    /*状态*/
    public function  setgoodsbycode(){

        $code=$this->request->param('id');
        $status=$this->request->param('status');

        $info=Db::name('aa_photolog')->where(['id'=>$code])->find();

        if (!$info){
            $this->error('操作失败');
        }


        $this->error('当前无法操作！');


        Db::name('aa_photolog')->where(['id'=>$code])->update(['status'=>$status]);

        $user=$this->user;


        $logs['name']=$user['nickname'];
        $logs['desc']='修改状态为:'.$this->getusertype($status);
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$info['id'];
        $logs['uid']=$user['id'];
        $logs['cid']=$info['caseid'];

        Db::name('aa_photolpg_log')->data($logs)->insert();

        $this->success('请求成功!');
    }


    /*状态*/
    public function  setgoodsstatusbycode(){

        $code=$this->request->param('code');
        $status=$this->request->param('status');

        $info=Db::name('aa_photolog')->where(['id'=>$code])->find();

        if (!$info){
            $this->error('操作失败');
        }


        Db::name('aa_photolog')->where(['id'=>$info['id']])->update(['status'=>$status]);

        $user=$this->user;


        $logs['name']=$user['user_nickname'];
        $logs['desc']='修改状态为:'.$status==1?'入库':'出库';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$info['id'];
        $logs['uid']=$user['id'];

        $logs['cid']=$info['caseid'];
        Db::name('aa_photolpg_log')->data($logs)->insert();

        $this->success('请求成功!');
    }


    /*状态*/
    public function  setgoodsstatusbycodeb(){

        $code=$this->request->param('code');
        $status=$this->request->param('status');

        $info=Db::name('aa_photolog')->where(['id'=>$code])->find();

        if (!$info){
            $this->error('操作失败');
        }


        Db::name('aa_photolog')->where(['id'=>$info['id']])->update(['status'.$status=>1]);

        $user=$this->user;


        $logs['name']=$user['user_nickname'];
        $logs['desc']='修改状态为:'.$this->getstatustype($status);
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$info['id'];
        $logs['uid']=$user['id'];

        $logs['cid']=$info['caseid'];
        Db::name('aa_photolpg_log')->data($logs)->insert();


        $this->autosavestatust($info['id']);


        $this->success('请求成功!');
    }

    /** 自动计算订单状态 */
    public function autosavestatust($id){

        $info=Db::name('aa_photolog')->where(['id'=>$id])->find();

        $ss=1;

        if ($info['model']=='是'&&$info['status4']==1){
            $ss=1;
        }
        if ($info['modelinfo']=='是'&&$info['status5']==1){
            $ss=1;
        }
        if ($info['modelvideo']=='是'&&$info['status11']==1){
            $ss=1;
        }

        if ($info['goodinfo']=='是'&&$info['status7']==1){
            $ss=1;
        }
        if ($info['material']=='是'&&$info['status9']==1){
            $ss=1;
        }
        if ($info['goodb']=='是'&&$info['status13']==1){
            $ss=1;
        }
        if ($info['all']=='是'&&$info['status8']==1){
            $ss=1;
        }


        if ($info['da']=='是'&&$info['status14']==1){
            $ss=1;
        }
        if ($info['dc']=='是'&&$info['status17']==1){
            $ss=1;
        }

        if ($info['db']=='是'&&$info['status15']==1){
            $ss=1;
        }

        if ($info['goodvideo']=='是'&&$info['status12']==1){
            $ss=1;
        }

        //反向

        if ($info['model']=='是'&&$info['status4']!=1){
            $ss=0;
        }
        if ($info['modelinfo']=='是'&&$info['status5']!=1){
            $ss=0;
        }
        if ($info['modelvideo']=='是'&&$info['status11']!=1){
            $ss=0;
        }

        if ($info['goodinfo']=='是'&&$info['status7']!=1){
            $ss=0;
        }
        if ($info['material']=='是'&&$info['status9']!=1){
            $ss=0;
        }
        if ($info['goodb']=='是'&&$info['status13']!=1){
            $ss=0;
        }
        if ($info['all']=='是'&&$info['status8']!=1){
            $ss=0;
        }


        if ($info['da']=='是'&&$info['status14']!=1){
            $ss=0;
        }
        if ($info['dc']=='是'&&$info['status17']!=1){
            $ss=0;
        }

        if ($info['db']=='是'&&$info['status15']!=1){
            $ss=0;
        }

        if ($info['goodvideo']=='是'&&$info['status12']!=1){
            $ss=0;
        }

        if($ss==1){
            Db::name('aa_photolog')->where(['id'=>$id])->update(['status'=>2]);
        }



    }




    public function setgoodcodebyid(){

        $code=$this->request->param('code');
        $id=$this->request->param('id');

        $info=Db::name('aa_photolog')->where(['id'=>$id])->find();

        if (!$info){
            $this->error('操作失败');
        }

        Db::name('aa_photolog')->where(['id'=>$id])->update(['code'=>$code]);

        $user=$this->user;


        $logs['name']=$user['user_nickname'];
        $logs['desc']='修改了code';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$info['id'];
        $logs['uid']=$user['id'];

        $logs['cid']=$info['caseid'];
        Db::name('aa_photolpg_log')->data($logs)->insert();

        $this->success('操作成功!');


    }

    /**
    dtimgs,sximgs
     */
    public function setgoodsphone(){
        $id=$this->request->param('id');
        $dtimgs=$this->request->param('dtimgs','');
        $sximgs=$this->request->param('sximgs','');

        if (strlen($dtimgs)<10||strlen($sximgs)<10 ){
            $this->error('请上传信息');
        }

        Db::name('aa_photolog')->where(['id'=>$id])->update(['dtimgs'=>$dtimgs,'sximgs'=>$sximgs,'status6'=>1]);

        $user=$this->user;


        $logs['name']=$user['user_nickname'];
        $logs['desc']='修改了水洗标';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];

        $info=Db::name('aa_photolog')->where(['id'=>$id])->find();
        $logs['cid']=$info['caseid'];


        Db::name('aa_photolpg_log')->data($logs)->insert();


        $this->success('操作成功!');
    }



    public function getstatustype($id){

        switch ($id){
            case 1:
                return '留仓状态';
                break;

            case 2:
                return '出库状态';
                break;
            case 3:
                return '货品异常';
                break;
            case 4:
                return '模特图完成';
                break;
            case 5:
                return '模特细节图完成';
                break;
            case 6:
                return '吊牌水洗完成';
                break;
            case 7:
                return '产品图完成';
                break;
            case 8:
                return '细节图完成';
                break;
            case 9:
                return '面料图完成';
                break;
            case 11:
                return '模特视频完成';
                break;
            case 12:
                return '产品视频完成';
                break;

        }

    }


    public function getmycase(){

        $uid=$this->getUserId();
        $user=Db::name('user')->where(['id'=>$uid])->find();


        //用户是商户
        if ($user['merhc']>0){
            $list=Db::name('aa_case')
                ->where(['merch'=>$user['merhc']])
                ->order('id desc')->select();
        }

        if ($user['employees']>0){

            $list=Db::name('aa_case')->order('id desc')->select();
        }


        $arr=[];
        foreach ($list as &$key){

            $in=Db::name('aa_photolog')->where('status','gt',0)->where(['caseid'=>$key['id']])->count();

            $all=Db::name('aa_photolog')->where(['caseid'=>$key['id']])->count();

            $key['in']=$in;
            $key['all']=$all;

            $key['djs']=strtotime($key['endtime'])-time();
            $key['startime']=date('Y.m.d日',strtotime($key['startime']));
            $key['endtime']=date('Y.m.d日',strtotime($key['endtime']));
            $merch=Db::name('aa_merch')->where(['id'=>$key['merch']])->find();

            $key['logo']=cmf_get_image_url($merch['logo']);

            $arr[]=$key;
        }

        $this->success('个人中心!',$arr);
    }



    public function getmerch(){

        $uid=$this->getUserId();
        $user=Db::name('user')->where(['id'=>$uid])->find();


        if ($user['employees']>0){

            $list=Db::name('aa_merch')->order('id desc')->select();

            $this->success('全部商户!',$list);
        }



        //用户是商户
        if ($user['merhc']>0){

            $list=Db::name('aa_merch')->where('id','eq',$user['merhc'])->select();

            $this->success('部分!',$list);
        }




        $this->error('没有信息');

    }


    //32132
    public function getmycases(){

        function getUserCaseList($map, $order, $limit, $user) {
            if ($user['merhc'] > 0) {
                $map[] = ['merch', 'eq', $user['merhc']];
            }
            $list = Db::name('aa_case')
                ->where($map)
                ->order($order)
                ->limit($limit)
                ->select();
            $count = Db::name('aa_case')
                ->where($map)
                ->count();
            $map[] = ['status', 'lt', 1];
            $count1 = Db::name('aa_case')
                ->where($map)
                ->count();
            return ['list' => $list, 'count' => $count, 'count1' => $count1];
        }

        $uid = $this->getUserId();
        $user = Db::name('user')->where(['id' => $uid])->find();

        $map[] = ['id', 'gt', 0];

        $merch = $this->request->param('merch');
        if ($merch) {
            $map[] = ['merch', 'eq', $merch];
        }

        $startime = $this->request->param('startime');
        if ($startime) {
            $map[] = ['timea', 'gt', strtotime($startime)];
        }

        $endtime = $this->request->param('endtime');
        if ($endtime) {
            $map[] = ['timeb', 'lt', strtotime($endtime)];
        }

        $orde = $this->request->param('order', '0');
        $order = 'id desc';
        if ($orde == 1) {
            $order = 'endtimes asc';
        }
        if ($orde == 2) {
            $order = 'count desc';
        }
        if ($orde == 3) {
            $order = 'count asc';
        }
        if ($orde == 4) {
            $order = 'id desc';
        }

        $page = $this->request->param('page', 1);
        $limit = ($page - 1) * 10 . ',10';

// 调用提取的函数
        $result = getUserCaseList($map, $order, $limit, $user);
        $list = $result['list'];
        $count = $result['count'];
        $count1 = $result['count1'];

        $arr = [];
        foreach ($list as &$key) {
            $lasttime = Db::name('aa_photolpg_log')->where('cid', 'eq', $key['id'])->order('id desc')->find();

            if ($lasttime) {
                $key['startime'] =$lasttime['time'];
            }else{
                $key['startime'] =date('Y-m-d H:i',$key['time'] );
            }

            //全部内容
            $all = Db::name('aa_photolog')->where(['caseid' => $key['id']])->count();
            $key['all'] = $all;

            //已完成
            $in = Db::name('aa_photolog')->where('status', 'gt', 1)->where(['caseid' => $key['id']])->count();
            $key['ok'] = $in;


            //已完成
            $inb = Db::name('aa_photolog')
                ->where('dapei', 'neq', 1)->where('status3', 'neq', 1)
                ->where('status', 'gt', 1)->where(['caseid' => $key['id']])->count();
            $key['okb'] = $inb;

            //未完成
            $in1 = Db::name('aa_photolog')
                ->where('status', 'lt', 2)
                ->where('dapei', 'neq', 1)->where('status3', 'neq', 1)
                ->where(['caseid' => $key['id']])
                ->count();
            $key['no'] = $in1;

            //入库
            $count_in = Db::name('aa_photolog')->where('caseid', 'eq', $key['id'])->where('status', 'gt', 0)->count();
            $key['in'] = intval($count_in);


            //出库
            $count_out = Db::name('aa_photolog')
                ->where('caseid', 'eq', $key['id'])
                ->where('status', 'eq', 5)->count();
            $key['out'] = intval($count_out);


            //搭配
            $count_out = Db::name('aa_photolog')
                ->where('caseid', 'eq', $key['id'])
                ->where('dapei', 'eq', 1)->count();
            $key['dapei'] = intval($count_out);

            //异常
            $count_out = Db::name('aa_photolog')
                ->where('caseid', 'eq', $key['id'])
                ->where('status3', 'eq', 1)->count();
            $key['zy'] = intval($count_out);

            //如果是全部完成  则修改为完成
            if ($key['ok'] == $key['all'] && $all > 0) {
                Db::name('aa_case')->where(['id' => $key['id']])->update(['plan' => 3]);
                $key['plan'] = 3;
            }



            $key['plan_v'] = getnewtypes($key['plan']);
            $arr[] = $key;
        }

        $this->success('个人中心!', ['list' => $arr, 'show1' => $count, 'show2' => $count1]);

    }



    // 可转换订单
    public function getcanzh(){

        $id=$this->request->param('id');

        $info=Db::name('aa_photolog')->where(['id'=>$id])->find();

        $case=Db::name('aa_case')->where(['id'=>$info['caseid']])->find();

        $list=Db::name('aa_case')
            ->where(['merch'=>$case['merch']])
            ->order('id desc')
            ->limit(6)
            ->select();


        $this->success('个人中心!',$list);
    }

    //转换到其他项目

    public function toothercate(){

        $id=$this->request->param('id');
        $caseid=$this->request->param('caseid');
        $infoid=$this->request->param('infoid');


        Db::name('aa_photolog')->where(['id'=>$id])->update(['caseid'=>$caseid]);

        Db::name('aa_photolog')->where(['id'=>$infoid])->delete();




        $this->success('转换成功！!');

    }
    /**
     *  操作记录
     */
    public function getlogsbyid(){

        $id=$this->request->param('id');

        $list=Db::name('aa_photolpg_log')
            ->where(['pid'=>$id])
            ->order('id desc')
            ->select();

        $this->success('转换成功！!',$list);
    }


    /**
     *上传图片
     */
    //上传图片 并修改状态
    public function two(){



        $id=$this->request->param('id');
        $uid=$this->request->param('uid');




        $imgurl=$http.$_SERVER['HTTP_HOST'].'/upload/'.date('Ymd').'/'.$url;



        Db::name('aa_photolog')->where(['id'=>$id])->update(['img'=>$imgurl,'status1'=>1,'status'=>1]);

        $infos=Db::name('aa_photolog')->where(['id'=>$id])->find();

        $user=Db::name('user')->where(['id'=>$uid])->find();

        $logs['name']=$user['user_nickname'];
        $logs['desc']='修改状态为:入库';
        $logs['time']=date('Y-m-d H:i');
        $logs['pid']=$id;
        $logs['uid']=$user['id'];
        $logs['cid']=$infos['caseid'];
        Db::name('aa_photolpg_log')->data($logs)->insert();



        //入库完成比大于50  则为制作中
        $isnoin=Db::name('aa_photolog')
            ->where('caseid','eq',$infos['caseid'])
            ->where('status','lt',1)
            ->count();

        $isnoin2=Db::name('aa_photolog')
            ->where('caseid','eq',$infos['caseid'])
            ->count();

        if (intval($isnoin*2)>$isnoin2){
            Db::name('aa_case')
                ->where('id','eq',$infos['caseid'])
                ->update(['plan'=>2]);
        }

        //如果都是不拍摄 则修改为  完成和搭配


        $this->setdapeibyid($id);

        $this->success("恭喜您,API访问成功!", ['img'=>$imgurl]);
    }



}